package com.yio.wifi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiManager;
import android.os.Parcelable;

/*
 *  @项目名：  wificonnector
 *  @包名：    com.yio.wifi
 *  @文件名:   NetworkReceiver
 *  @创建者:   yio
 *  @创建时间:  2016/9/6 13:52
 *  @描述：    网络状态改变广播接收类-
 */
public class NetworkReceiver
        extends BroadcastReceiver {
    private static final String TAG = "NetworkReceiver";
    public NetEvent event = WifiFragment.event;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction()
                .equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    L.i("WIFI_STATE_DISABLED");
                    break;
                case WifiManager.WIFI_STATE_ENABLED:
                    L.i("WIFI_STATE_ENABLED");
                    break;
                case WifiManager.WIFI_STATE_ENABLING:
                    L.i("WIFI_STATE_ENABLING");
                    if (event != null) {
                        event.onOpenWifi();
                    }
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    L.i("WIFI_STATE_DISABLING");
                    if (event != null) {
                        event.onCloseWifi();
                    }
                    break;
            }
        } else if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            L.i("wifi扫描到wifi信息");
            if (event != null) {
                event.onScanCompleted();
            }
        } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
            if (state == SupplicantState.COMPLETED) {
//                    event.onConnectSuccess();
            } else if (state == SupplicantState.DISCONNECTED) {
                if (event != null) {
                    event.onConnectChange(2);
                }
            }
        } else if (WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION.equals(intent.getAction())) {
            L.e(TAG, "wifi 请求 连接状态改变");
        } else if (intent.getAction()
                .equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                NetworkInfo.State state = networkInfo.getState();
                int connectType = 0;
                if (state == NetworkInfo.State.CONNECTED) {
                    connectType = 1;
                    L.i(TAG, "CONNECTED");
                } else if (state == NetworkInfo.State.CONNECTING) {
                    connectType = 3;
                } else if (state == NetworkInfo.State.DISCONNECTED) {
                    L.i(TAG, "DISCONNECTED");
                }
                L.i(TAG, "state=" + state);
                // 连接网络是否成功 接口回调
                if (event != null) {
                    event.onConnectChange(connectType);
                }
            }
        }
    }

    // 自定义接口
    public interface NetEvent {
        void onOpenWifi();

        void onScanCompleted();

        void onCloseWifi();

        void onConnectChange(int connectType);
    }

}
